import {app, BrowserWindow, Menu, MenuItem, MenuItemConstructorOptions} from "electron";
import {isMac, loadSettings, MessageType, refreshDNS, Settings} from "../common/utils";
import {showAboutDialog, showHelpDialog} from "./dialogs";
import {exportHosts, importHosts, showMessageInMain} from "./utils";
import {EventKeys} from "../common/EventKeys";

const macMenu = (): MenuItemConstructorOptions[] | MenuItem[] => [
    {
        label: app.getName(),
        submenu: [
            {
                label: '关于',
                click: (item: MenuItem, window: BrowserWindow) => {
                    showAboutDialog(window)
                }
            },
            {type: "separator"},
            {role: "services", label: '服务'},
            {type: "separator"},
            {role: "hide", label: '隐藏'},
            {role: "hideOthers", label: '隐藏其他'},
            {role: "unhide", label: '全部显示'},
            {type: "separator"},
            {role: "quit", label: '退出'}
        ]
    }
];

export const appMenus = (win?: BrowserWindow) => Menu.buildFromTemplate([
    ...(isMac() ? macMenu() : []),
    {
        label: "文件(&F)",
        role: "fileMenu",
        submenu: [
            {
                label: "新建(&N)",
                accelerator: "CmdOrCtrl+N",
                click: (item, window) => {
                    (win || window)?.webContents?.send(EventKeys.addNewRule);
                }
            },
            {
                label: "导入(&I)",
                accelerator: "CmdOrCtrl+I",
                click: (item, window) => {
                    importHosts(win || window)
                }
            },
            {
                label: "导出(&O)",
                accelerator: "CmdOrCtrl+O",
                click: (item, window) => {
                    exportHosts(win || window)
                }
            },
            {
                label: "恢复默认规则",
                accelerator: "CmdOrCtrl+Shift+D",
                click: (item, window) => {
                    (win || window)?.webContents?.send(EventKeys.reset2default)
                }
            },
            // ...(!isMac() ? [{label: "退出", role: "quit"}] : [])
        ]
    },
    {
        label: "工具(&T)",
        submenu: [
            {
                label: "刷新DNS缓存",
                accelerator: "CmdOrCtrl+Shift+R",
                click: () => {
                    refreshDNS()
                        .then(msg => showMessageInMain(msg, MessageType.success))
                        .catch(msg => showMessageInMain(msg, MessageType.error))
                }
            }
        ]
    },
    {
        label: "帮助(&H)",
        submenu: [
            {
                label: "打开调试工具",
                role: "toggleDevTools"
            },
            {
                label: "帮助",
                role: "help",
                accelerator: "Ctrl+H",
                click: (item, window) => {
                    showHelpDialog(win || window)
                }
            }
        ]
    }
]);
export const editContextMenu = Menu.buildFromTemplate([
    {
        label: "复制",
        role: "copy",
        accelerator: "CommandOrControl+C"
    },
    {
        label: "剪切",
        role: "cut",
        accelerator: "CommandOrControl+X"
    },
    {
        label: "粘贴",
        role: "paste",
        accelerator: "CommandOrControl+V"
    },
    {
        type: "separator"
    },
    {
        label: "撤销",
        role: "undo",
        accelerator: "CommandOrControl+Z"
    },
    {
        label: "重做",
        role: "redo",
        accelerator: "CommandOrControl+Shift+Z"
    },
    {
        type: "separator"
    },
    {
        label: "全选",
        role: "selectAll",
        accelerator: "CommandOrControl+A"
    }
]);
export const trayMenu = (sender: Electron.WebContents, settings: Settings, win: BrowserWindow) => Menu.buildFromTemplate([
    {
        label: "显示/隐藏",
        click: () => win.isVisible() ? win.hide() : win.show()
    },
    {type: "separator"},
    ...Object.values((settings || loadSettings()).hosts).map(host => new MenuItem(({
        label: host.name,
        checked: host.isActive,
        type: 'checkbox',
        click: () => {
            sender.send(EventKeys.applyHost, host)
        }
    }))),
    {type: "separator"},
    {
        label: "工具",
        submenu: [
            {
                label: "刷新 DNS 缓存", click: () => {
                    refreshDNS()
                        .then(msg => showMessageInMain(msg, MessageType.success))
                        .catch(msg => showMessageInMain(msg, MessageType.error))
                }
            }
        ]
    },
    {type: "separator"},
    {
        label: '关于',
        click: (item, window) => {
            showAboutDialog(window)
        }
    },
    {
        label: '帮助', role: "help",
        click: (item, window) => {
            showHelpDialog(window)
        }
    },
    {
        label: "重启应用", click: () => {
            app.quit();
            app.exit();
            app.relaunch()
        }
    },
    {label: '退出', role: "quit"}
]);